package com.example.administrator.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.myapplication.body.bodyActivity;
import com.example.administrator.myapplication.evaluate.EvaluateMainActivity;
import com.example.administrator.myapplication.flexibility.flexibilityActivity;
import com.example.administrator.myapplication.heart.heartActivity;
import com.example.administrator.myapplication.login.LoginActivity;
import com.example.administrator.myapplication.muscle.muscleActivity;
import com.example.administrator.myapplication.set.PersonalCenter.PersonalCenterTestedActivity;
import com.example.administrator.myapplication.set.PersonalCenter.PersonalCenterunTestActivity;


public class HomeActivity extends AppCompatActivity {

    private LinearLayout imgHeart;
    private LinearLayout imgMuscle;
    private LinearLayout imgFlexibility;
    private LinearLayout imgBody;
    private Button bt_total_evaluate;
    private TextView tv_heard_entrance;
    private TextView tv_muscle_entrance;
    private TextView tv_soft_entrance;
    private TextView tv_body_entrance;
    private BmobApplication home_apps;
    private SharedPreferences sp;
    private Toolbar toolbar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        init();


        toolbar= (Toolbar) findViewById(R.id.toolbar_Home);
        toolbar.setTitle("大学生健康体适能检测系统");
        toolbar.setTitleTextColor(Color.parseColor("#ffffff"));
        toolbar.setTitleMarginStart(35);
        toolbar.setNavigationIcon(R.mipmap.custom_set);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Boolean login_flag=sp.getBoolean("login",false);
                if(!login_flag){
                    Intent intent =new Intent(HomeActivity.this,LoginActivity.class);
                    startActivity(intent);
                }else {
                    if(!(home_apps.getHeart_time_result().isEmpty()||home_apps.getHeart_rate_result().isEmpty()||home_apps.getBody_weight_result().isEmpty()&&
                            home_apps.getBody_height_result().isEmpty()||home_apps.getSoft_result().isEmpty()||home_apps.getMuscle_result().isEmpty())) {
                        Intent intent = new Intent(HomeActivity.this, PersonalCenterTestedActivity.class);
                        startActivity(intent);
                    }else{
                        Intent intent =new Intent(HomeActivity.this, PersonalCenterunTestActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });




        imgHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomeActivity.this, heartActivity.class);
                startActivity(intent);

            }
        });
        imgMuscle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomeActivity.this, muscleActivity.class);
                startActivity(intent);
            }
        });
        imgFlexibility.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomeActivity.this, flexibilityActivity.class);
                startActivity(intent);
            }
        });
        imgBody.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomeActivity.this, bodyActivity.class);
                startActivity(intent);
            }
        });

        bt_total_evaluate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否登录过
                Boolean login_flag=sp.getBoolean("login",false);
                if(!login_flag){
                    Intent intent =new Intent(HomeActivity.this,LoginActivity.class);
                    startActivity(intent);
                }else {
                    if(!(home_apps.getHeart_time_result().isEmpty()||home_apps.getHeart_rate_result().isEmpty()||home_apps.getBody_weight_result().isEmpty()&&
                            home_apps.getBody_height_result().isEmpty()||home_apps.getSoft_result().isEmpty()||home_apps.getMuscle_result().isEmpty())) {
                        Intent intent = new Intent(HomeActivity.this, EvaluateMainActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(HomeActivity.this,"有项目没有测试完",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });


    }


    public void init(){
        imgHeart = (LinearLayout) findViewById(R.id.main_img_heart);
        imgMuscle = (LinearLayout) findViewById(R.id.main_img_muscle);
        imgFlexibility = (LinearLayout) findViewById(R.id.main_img_flex);
        imgBody = (LinearLayout) findViewById(R.id.main_img_body);

        tv_body_entrance= (TextView) findViewById(R.id.tv_body_entrance);
        tv_heard_entrance= (TextView) findViewById(R.id.tv_heard_entrance);
        tv_muscle_entrance= (TextView) findViewById(R.id.tv_muscle_entrance);
        tv_soft_entrance= (TextView) findViewById(R.id.tv_soft_entrance);

        home_apps= (BmobApplication) getApplication();
        bt_total_evaluate= (Button) findViewById(R.id.main_bt_multitest);
        sp=getSharedPreferences("login",MODE_PRIVATE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        //判断测试项目是否有测试，如果有，则显示红色
        if(!(home_apps.getHeart_rate_result().isEmpty()&&home_apps.getHeart_time_result().isEmpty())){
            tv_heard_entrance.setTextColor(Color.parseColor("#ff0000"));
        }
        if(!home_apps.getMuscle_result().isEmpty()){
            tv_muscle_entrance.setTextColor(Color.parseColor("#ff0000"));
        }
        if(!home_apps.getSoft_result().isEmpty()){
            tv_soft_entrance.setTextColor(Color.parseColor("#ff0000"));
        }
        if(!(home_apps.getBody_height_result().isEmpty()&&home_apps.getBody_weight_result().isEmpty())){
            tv_body_entrance.setTextColor(Color.parseColor("#ff0000"));
        }
    }
}
